import { GaRegExp, ruleInject, ruleInjectable } from "../InjectionRule";

export type XmlData = [string, [string, string][], (string | XmlData)[], string];

const xmlContentRule = new GaRegExp([
	"(!",
	(save, origin, rule, str, index, char, length) => {
		const ruleInfo = rule.info;
		if (ruleInfo.saveStr === "\\") {
			ruleInfo.saveStr = undefined;
			return true;
		} else if (char === "\\") {
			ruleInfo.saveStr = "\\";
			return "next";
		} else {
			if (char === "<") {
				return false;
			} else if (char === ">") {
				return false;
			} else {
				return true;
			}
		}
	},
	"*)",
]);

const labelName = "(![\\_a-zA-Z\\u4e00-\\u9fa5][\\.\\-\\:\\_a-zA-Z0-9\\u4e00-\\u9fa5]*)";

@ruleInjectable()
export default class xmlRule extends GaRegExp {

	@ruleInject() private stringRule!: GaRegExp;

	public RuleInit() {
		this.ParseRule([
			"(~\\s*)", "\\<", labelName, "(~\\s*)", "(", labelName, "(~\\s*)", "=", "(~\\s*)", this.stringRule, "(~\\s*)", ")*", "(", "\\/", "(~\\s*)", ")?", "\\>",
			"(-", "(", "(&", "\\s*", this, "\\s*", ")*", "|", xmlContentRule, ")", "\\</", labelName, "(~\\s*)", "\\>", ")?", "(~\\s*)",
		]);
	}

}
